package testApplets;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Arrays;

import processing.core.PApplet;
import simpleGeom.*;

public class FinalSplitApplet extends PApplet {
	// Mesh mesh;
	String path="/Users/caad/Desktop/Final Shape/Iteration10/";
	Box3D bds=new Box3D(0f,0f,-1.75f,2.0f,3.2f,0f);
	float[] cuts;
	
	public void loadCuts(){
		String filePath=path+"/cuts.txt";
		String line=this.loadStrings(filePath)[0];
		String[]numbersStr=line.split(",");
		cuts=new float[numbersStr.length];
		for (int i=0;i<cuts.length;i++){
			cuts[i]=Float.valueOf(numbersStr[i]);
		}
		//Arrays.copyOf(original, newLength);
	}
	int iCut=7;
	public void setup() {
		loadCuts();
		/*clip(cuts[iCut],cuts[iCut+1]);*/
		clip(1.482f,3);
		//System.gc();
		/*Mesh meshInput = new Mesh(this.selectInput());
		meshInput.loadAddMesh(this.selectInput());
		meshInput.saveMesh(this.selectOutput());*/
		this.exit();
	}
 
	public void scaleToBounds(){
		Mesh meshInput = new Mesh(this.selectInput());
		Box3D mBds=meshInput.getBounds();
		println(mBds);
		
		
		/*float scaleX=bds.getDimX()/mBds.getDimX();
		float scaleY=bds.getDimY()/mBds.getDimY();
		float scaleZ=bds.getDimZ()/mBds.getDimZ();
		println("x: "+scaleX);
		println("y: "+scaleY);
		println("z: "+scaleZ);
		meshInput.scale(scaleX,scaleY,scaleZ);
		mBds=meshInput.getBounds();
		meshInput.translate(bds.x1-mBds.x1, bds.y1-mBds.y1, bds.z1-mBds.z1);
		meshInput.saveMesh(this.selectOutput());*/
	}
	public void clip(float y1,float y2){
		Mesh meshInput;
		
		meshInput = new Mesh(this.selectInput());
		Box3D mBds =meshInput.getBounds();
		
		//float y2=mBds.y2;
		//float y2=y1+2f;//0.4f
		println(meshInput.getBounds());
		println("meshInput");
		meshInput.triangulate();
		println("triangulated");
		meshInput.construct();
		println("constructed");
		Plane plane=new Plane(new Vec3D(0,y1,0),new Vec3D(0,1,0));
		meshInput.trimMesh(plane);
		println("trimmed 1");
		meshInput.triangulate();
		 plane=new Plane(new Vec3D(0,y2,0),new Vec3D(0,-1,0));
		meshInput.trimMesh(plane);
		meshInput.construct();
		println("trimmed 2");
		meshInput.triangulate();
		meshInput.saveMesh((this.selectOutput())+"_"+y1+"_"+y2+".obj",false);
		println("saved");
		
	}
	public void draw(){
		if (flagLoad){
			String input=this.selectInput();
			Mesh mesh=new Mesh(input);
			println(input);
			println(mesh.getBounds());
			flagLoad=false;
		}
	}

 boolean flagLoad;
 
	public void keyPressed(){
		flagLoad=true;
	}

}
